package com.copera.cn.kaka;

import android.content.Context;
import android.content.res.ColorStateList;
import android.content.res.Resources;
import android.graphics.Color;
import android.graphics.Typeface;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;


public class FragmentIndicator extends LinearLayout implements OnClickListener {

	private int mDefaultIndicator = 0;

	private static int mCurIndicator;

	private static View[] mIndicators;

	private OnIndicateListener mOnIndicateListener;

	private static final String TAG_ICON_0 = "icon_tag_0";
	private static final String TAG_ICON_1 = "icon_tag_1";
	private static final String TAG_ICON_2 = "icon_tag_2";
	

	private static final String TAG_TEXT_0 = "text_tag_0";
	private static final String TAG_TEXT_1 = "text_tag_1";
	private static final String TAG_TEXT_2 = "text_tag_2";
	
	
	

	private FragmentIndicator(Context context) {
		super(context);
	}

	public FragmentIndicator(Context context, AttributeSet attrs) {
		super(context, attrs);

		mCurIndicator = mDefaultIndicator;
		setOrientation(LinearLayout.HORIZONTAL);
		init();
	}

	private View createIndicator(int iconResID, int stringResID, int stringColor,   
            String iconTag, String textTag) {
		LinearLayout view = new LinearLayout(getContext());  
        view.setOrientation(LinearLayout.VERTICAL);  
        view.setLayoutParams(new LinearLayout.LayoutParams(  
                LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, 1));  
        view.setGravity(Gravity.CENTER_HORIZONTAL);  
      
        ImageView iconView = new ImageView(getContext());  
        iconView.setTag(iconTag); 
        LinearLayout.LayoutParams layoutParams =new LinearLayout.LayoutParams(
				LayoutParams.WRAP_CONTENT, 120, 1);
        layoutParams.topMargin=15;
        layoutParams.leftMargin=-80;
        iconView.setLayoutParams(layoutParams);  
        iconView.setImageResource(iconResID);  
      
        TextView textView = new TextView(getContext());  
        textView.setTag(textTag);  
        LinearLayout.LayoutParams layoutParams1 =new LinearLayout.LayoutParams(
				LayoutParams.WRAP_CONTENT, 120, 1);
        
        layoutParams1.topMargin=-70;
        layoutParams1.leftMargin=20;
        textView.setLayoutParams(layoutParams1);  
        textView.setTypeface(null,Typeface.BOLD);
        Resources resource = (Resources) getContext().getResources();  
        ColorStateList csl = (ColorStateList) resource.getColorStateList(R.color.font);  
        if (csl != null) {  
        	textView.setTextColor(csl);  
        }
        //textView.setTextColor(Color.parseColor("#545454"));  
        textView.setTextSize(18.0f);  
        textView.setText(stringResID);  
        //textView.setTextAppearance(getContext(), R.style.boldText);
        view.addView(iconView);  
        view.addView(textView);  
      
        return view;  

	}
    
	private void init() {
		mIndicators = new View[3];
		mIndicators[0] = createIndicator(R.drawable.bar003,R.string.shop,
				 R.color.check, TAG_ICON_0, TAG_TEXT_0);
		mIndicators[0].setBackgroundResource(R.color.check);
		mIndicators[0].setTag(Integer.valueOf(0));
		mIndicators[0].setOnClickListener(this);
		addView(mIndicators[0]);
		mIndicators[1] = createIndicator(R.drawable.bar002,R.string.track_record,
				 R.color.nocheck, TAG_ICON_1, TAG_TEXT_1);
		mIndicators[0].setBackgroundResource(R.color.nocheck);
		mIndicators[1].setTag(Integer.valueOf(1));
		mIndicators[1].setOnClickListener(this);
		addView(mIndicators[1]);
		mIndicators[2] = createIndicator(R.drawable.bar001,R.string.exchange,
				 R.color.nocheck, TAG_ICON_2, TAG_TEXT_2);
		mIndicators[0].setBackgroundResource(R.color.nocheck);
		mIndicators[2].setTag(Integer.valueOf(2));
		mIndicators[2].setOnClickListener(this);
		addView(mIndicators[2]);
		
	}

	public static void setIndicator(int which) {
		// clear previous status.
		mIndicators[mCurIndicator].setBackgroundColor(Color.alpha(0));
		ImageView prevIcon;
		TextView prevText;
		switch(mCurIndicator) {
		case 0:
			prevIcon =(ImageView) mIndicators[mCurIndicator].findViewWithTag(TAG_ICON_0);
			prevIcon.setImageResource(R.drawable.bar003);
			prevText = (TextView) mIndicators[mCurIndicator].findViewWithTag(TAG_TEXT_0);
			//prevText.setTextColor(COLOR_UNSELECT);
			break;
		case 1:
			prevIcon =(ImageView) mIndicators[mCurIndicator].findViewWithTag(TAG_ICON_1);
			prevIcon.setImageResource(R.drawable.bar002);
			prevText = (TextView) mIndicators[mCurIndicator].findViewWithTag(TAG_TEXT_1);
			//prevText.setTextColor(COLOR_UNSELECT);
			break;
		case 2:
			prevIcon =(ImageView) mIndicators[mCurIndicator].findViewWithTag(TAG_ICON_2);
			prevIcon.setImageResource(R.drawable.bar001);
			prevText = (TextView) mIndicators[mCurIndicator].findViewWithTag(TAG_TEXT_2);
			//prevText.setTextColor(COLOR_UNSELECT);
			break;
		
		}
		
		// update current status.
		mIndicators[which].setBackgroundResource(R.color.check);
		ImageView currIcon;
		TextView currText;
		switch(which) {
		case 0:
			currIcon =(ImageView) mIndicators[which].findViewWithTag(TAG_ICON_0);
			currIcon.setImageResource(R.drawable.bar003);
			currText = (TextView) mIndicators[which].findViewWithTag(TAG_TEXT_0);
			//currText.setTextColor(COLOR_SELECT);
			break;
		case 1:
			currIcon =(ImageView) mIndicators[which].findViewWithTag(TAG_ICON_1);
			currIcon.setImageResource(R.drawable.bar002);
			currText = (TextView) mIndicators[which].findViewWithTag(TAG_TEXT_1);
			//currText.setTextColor(COLOR_SELECT);
			break;
		case 2:
			currIcon =(ImageView) mIndicators[which].findViewWithTag(TAG_ICON_2);
			currIcon.setImageResource(R.drawable.bar001);
			currText = (TextView) mIndicators[which].findViewWithTag(TAG_TEXT_2);
			//currText.setTextColor(COLOR_SELECT);
			break;
		
		}
		
		mCurIndicator = which;
	}
	public static void setIndicator(int which,String type) {
		// clear previous status.
		mIndicators[mCurIndicator].setBackgroundColor(Color.alpha(0));
		ImageView prevIcon;
		TextView prevText;
		switch(mCurIndicator) {
		case 0:
			prevIcon =(ImageView) mIndicators[mCurIndicator].findViewWithTag(TAG_ICON_0);
			prevIcon.setImageResource(R.drawable.bar003);
			prevText = (TextView) mIndicators[mCurIndicator].findViewWithTag(TAG_TEXT_0);
			//prevText.setTextColor(COLOR_UNSELECT);
			break;
		case 1:
			prevIcon =(ImageView) mIndicators[mCurIndicator].findViewWithTag(TAG_ICON_1);
			prevIcon.setImageResource(R.drawable.bar002);
			prevText = (TextView) mIndicators[mCurIndicator].findViewWithTag(TAG_TEXT_1);
			//prevText.setTextColor(COLOR_UNSELECT);
			break;
		case 2:
			prevIcon =(ImageView) mIndicators[mCurIndicator].findViewWithTag(TAG_ICON_2);
			prevIcon.setImageResource(R.drawable.bar001);
			prevText = (TextView) mIndicators[mCurIndicator].findViewWithTag(TAG_TEXT_2);
			//prevText.setTextColor(COLOR_UNSELECT);
			break;
		
		}
		if(type!=null&&!type.equals("1")){
			// update current status.
			mIndicators[which].setBackgroundResource(R.color.check);
			ImageView currIcon;
			TextView currText;
			switch(which) {
			case 0:
				currIcon =(ImageView) mIndicators[which].findViewWithTag(TAG_ICON_0);
				currIcon.setImageResource(R.drawable.bar003);
				currText = (TextView) mIndicators[which].findViewWithTag(TAG_TEXT_0);
				//currText.setTextColor(COLOR_SELECT);
				break;
			case 1:
				currIcon =(ImageView) mIndicators[which].findViewWithTag(TAG_ICON_1);
				currIcon.setImageResource(R.drawable.bar002);
				currText = (TextView) mIndicators[which].findViewWithTag(TAG_TEXT_1);
				//currText.setTextColor(COLOR_SELECT);
				break;
			case 2:
				currIcon =(ImageView) mIndicators[which].findViewWithTag(TAG_ICON_2);
				currIcon.setImageResource(R.drawable.bar001);
				currText = (TextView) mIndicators[which].findViewWithTag(TAG_TEXT_2);
				//currText.setTextColor(COLOR_SELECT);
				break;
			
			}
			
			mCurIndicator = which;
		}
		
	}
	public interface OnIndicateListener {
		public void onIndicate(View v, int which);
	}

	public void setOnIndicateListener(OnIndicateListener listener) {
		mOnIndicateListener = listener;
	}

	@Override
	public void onClick(View v) {
		if (mOnIndicateListener != null) {
			int tag = (Integer) v.getTag();
			switch (tag) {
			case 0:
				if (mCurIndicator != 0) {
					mOnIndicateListener.onIndicate(v, 0);
					setIndicator(0);
				}
				break;
			case 1:
				if (mCurIndicator != 1) {
					mOnIndicateListener.onIndicate(v, 1);
					setIndicator(1);
				}
				break;
			case 2:
				if (mCurIndicator != 2) {
					mOnIndicateListener.onIndicate(v, 2);
					setIndicator(2);
				}
				break;
			
			default:
				break;
			}
		}
	}
}
